
#! /usr/bin/python

import sys
import os
import time
import random
#sys.path.append('..'+os.sep+'trunk')
sys.path.append('../../../')
from spade import *
class Example(Agent.Agent):
    def _setup(self):
        pass
    def _process(self):
	 pass



class MyCongregation(Organization.Congregation):
    def _process(self):
	    time.sleep(2000)
    def onStart(self):
        if not self.owner:
            self.team=MyTeam(agent=self.myAgent, nick="autioner", name=self.myAgent.producto,agentList=[])
            self.addUnit(self.team)
    def leadershipTest(self,sender,desc): return True

class MyTeam(Unit.Team):
    def onStart(self):
        self.currentPrice=self.myAgent.precio
        self.price=self.myAgent.precio
        self.buyer=""
        self.setGoal({"Product":self.myAgent.producto,"StartPrice":self.price,"CurrentPrice":self.currentPrice,"Buyer":self.buyer})
        print {"Product":self.myAgent.producto,"StartPrice":self.price}
        self.setMinAgents(self.myAgent.min)

    def _process(self):
        t1=time.time()
        while time.time()-t1<self.myAgent.tiempo:
            m=self.receiveMessage()
            if m:
                cant=int(m.getContent())
                #print cant,self.price,self.currentPrice
                if cant>self.currentPrice and cant>=self.price:
                    self.currentPrice=cant
                    self.buyer=m.getSender().getName()
                    self.setGoal({"Product":self.myAgent.producto,"StartPrice":self.price,"CurrentPrice":self.currentPrice,"Buyer":self.buyer})
                    print {"Product":self.myAgent.producto,"StartPrice":self.price,"CurrentPrice":self.currentPrice,"Buyer":self.buyer}
        mess=ACLMessage.ACLMessage()
        mess.setPerformative("inform")
        #mess.setSender(self.myAgent.getAID())
        #mess.addReceiver(mens.getSender())
        mess.setContent(str({"CurrentPrice":self.currentPrice,"Buyer":self.buyer}))
        self.sendMessage(mess)
        print {"CurrentPrice":self.currentPrice,"Buyer":self.buyer}
        self.destroy()

    def entryUnitTest(self,sender,desc=None):
        return True

if __name__ == "__main__":
    agent="auctionman@apolo.dsic.upv.es"
    b = Example(agent,"pasword")
    b.start()
    #creamos la organizacion para la casa de subastas
    o=MyCongregation(agent=b,nick="auctionman",name="casasubastas")
    b.createOrganization(o)
    o.setGoal({"goal":"auction"})
    a="subasta"
    if len(sys.argv)>1:
        max=int(sys.argv[1])
    else: max=3
    lista=[]
    for n in xrange(0,max):
        lista.append("Producto"+str(n))
        agent = a+str(n)+"@apolo.dsic.upv.es"
        o.giveLeadership(agent)
        b = Example(agent,"pasword")
        b.producto="Producto"+str(n)
        b.precio=random.randint(100,200)
        #b.min=random.randint(1,6)
        b.min=1
        b.tiempo=random.randint(100,300)
        b.start()
        org=MyCongregation(b,nick=b.producto, name="casasubastas")
        b.joinOrganization(org)









